using FreeSql.DataAnnotations;

namespace SumerCoreDevOps.Domain.Entities;

/// <summary>
/// 用户实体
/// </summary>
[Table(Name = "users")]
[Index("idx_username", "Username", true)] // 唯一索引
[Index("idx_email", "Email", true)]
public class User
{
    /// <summary>
    /// 用户 ID
    /// </summary>
    [Column(IsPrimary = true, IsIdentity = true)]
    public long Id { get; set; }

    /// <summary>
    /// 用户名（登录名）
    /// </summary>
    [Column(StringLength = 100, IsNullable = false)]
    public string Username { get; set; } = null!;

    /// <summary>
    /// 显示名称
    /// </summary>
    [Column(StringLength = 200)]
    public string? DisplayName { get; set; }

    /// <summary>
    /// 邮箱
    /// </summary>
    [Column(StringLength = 200, IsNullable = false)]
    public string Email { get; set; } = null!;

    /// <summary>
    /// 密码哈希（使用 BCrypt 或 PBKDF2）
    /// </summary>
    [Column(StringLength = 500, IsNullable = false)]
    public string PasswordHash { get; set; } = null!;

    /// <summary>
    /// 密码盐
    /// </summary>
    [Column(StringLength = 100)]
    public string? PasswordSalt { get; set; }

    /// <summary>
    /// 手机号
    /// </summary>
    [Column(StringLength = 50)]
    public string? PhoneNumber { get; set; }

    /// <summary>
    /// 头像 URL
    /// </summary>
    [Column(StringLength = 500)]
    public string? AvatarUrl { get; set; }

    /// <summary>
    /// 是否启用
    /// </summary>
    [Column(IsNullable = false)]
    public bool IsEnabled { get; set; } = true;

    /// <summary>
    /// 是否已锁定
    /// </summary>
    [Column(IsNullable = false)]
    public bool IsLocked { get; set; } = false;

    /// <summary>
    /// 锁定原因
    /// </summary>
    [Column(StringLength = 500)]
    public string? LockReason { get; set; }

    /// <summary>
    /// 锁定时间
    /// </summary>
    public DateTime? LockedAt { get; set; }

    /// <summary>
    /// 失败登录次数
    /// </summary>
    [Column(IsNullable = false)]
    public int FailedLoginAttempts { get; set; } = 0;

    /// <summary>
    /// 最后登录时间
    /// </summary>
    public DateTime? LastLoginAt { get; set; }

    /// <summary>
    /// 最后登录 IP
    /// </summary>
    [Column(StringLength = 100)]
    public string? LastLoginIp { get; set; }

    /// <summary>
    /// 密码过期时间
    /// </summary>
    public DateTime? PasswordExpiresAt { get; set; }

    /// <summary>
    /// 是否必须修改密码
    /// </summary>
    [Column(IsNullable = false)]
    public bool MustChangePassword { get; set; } = false;

    /// <summary>
    /// 备注
    /// </summary>
    [Column(StringLength = 1000)]
    public string? Remarks { get; set; }

    /// <summary>
    /// 创建时间
    /// </summary>
    [Column(IsNullable = false)]
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;

    /// <summary>
    /// 创建者
    /// </summary>
    [Column(StringLength = 100)]
    public string? CreatedBy { get; set; }

    /// <summary>
    /// 更新时间
    /// </summary>
    public DateTime? UpdatedAt { get; set; }

    /// <summary>
    /// 更新者
    /// </summary>
    [Column(StringLength = 100)]
    public string? UpdatedBy { get; set; }

    /// <summary>
    /// 用户角色关联
    /// </summary>
    [Navigate(ManyToMany = typeof(UserRole))]
    public List<Role> Roles { get; set; } = new();
}
